//
//  NetworkManager+Extension.swift
//  SinaWeiBo
//
//  Created by Leon 笑 竹 on 2017/8/4.
//  Copyright © 2017年 Leon 笑 竹. All rights reserved.
//

import Foundation

extension NetworkManager {
    
    //封装新浪微博的网络请求方法
    ///
    /// - Parameters:
    ///   - since_id: 返回id比since_id大的微博
    ///   - max_id: 返回id小于或等于max_id的微博，默认为0
    func statusList(since_id:Int64 = 0, max_id : Int64 = 0, completion : @escaping (_ list : [[String : Any]]?, _ isSuccess: Bool) -> ()){
     
        //网络获取请求（首页）
        let url = "https://api.weibo.com/2/statuses/home_timeline.json"
//        let params = ["access_token" : "2.00Hx2rnGwRZyQDc647b541348IzmSE"]
        
        //int64 转换
        let params = ["since_id" : "\(since_id)", "max_id" : "\(max_id > 0 ? max_id - 1 : 0)"]
        
        //封装请求数据
        
        tokenRequest(methods: .GET, url: url, params: params as [String : AnyObject]) { (json, success) in
            
            let result = json?["statuses"] as? [[String: AnyObject]]
            
            completion(result, success)
            
        }
    }
    
    //未读的方法
    func unreadCount(completion : @escaping (_ count : Int) ->()){
        
        let url = "https://rm.api.weibo.com/2/remind/unread_count.json"
        guard let uid = uid else {
            return
        }
     
        let params = ["uid" : uid] as [String : AnyObject];
        
        tokenRequest(url: url, params: params) { (json, isSuccess) in
            
            let dict = json as? [String : AnyObject] ?? [:]
            let status = dict["status"] as? Int ?? 0
            
            completion(status)
        }
    }
}
